<template>
  <div>
    <van-nav-bar title="电商平台-登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="userInfo.mobile"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        required
        :rules="[
          { required: true, pattern: phoneTest, message: '请填写正确的手机号' },
        ]"
      />
      <van-field
        v-model="userInfo.code"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        required
        :rules="[
          { required: true, message: '请填写6位密码', pattern: parssWordTest },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="btnStatus"
          :disabled="btnStatus"
          loading-text="登录中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
/*
手机号,密码正则
*/
import { loaginAPI } from '@/api/index'
import Notify from '@/ui/Notify'
// getToken, removeToken
import { setToken } from '@/utils/token'
import mixin from '@/mixin/index'
const phoneTest =
  /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const parssWordTest = /^\d{6}$/
export default {
  name: 'HmttIndex',
  mixins: [mixin],
  data() {
    return {
      // 用户信息
      userInfo: {
        mobile: '13888888888',
        code: '246810',
      },
      phoneTest,
      parssWordTest,
      btnStatus: false,
    }
  },
  created() {},
  methods: {
    // 登录提交
    async onSubmit() {
      try {
        this.btnStatus = true
        const { mobile, code } = this.userInfo
        const res = await loaginAPI({ mobile, code })
        if (res.status === 201) {
          this.btnStatus = !this.btnStatus
          setToken(res.data.data.token) // 存token
          localStorage.setItem('refresh_token', res.data.data.refresh_token) // 存refresh_token,用于token过期后续签
          Notify({ type: 'success', message: '登录成功', duration: 500 })
          // push 跳转有回退，replace没回退
          // 判断有无操作未遂的页面，有的话跳转过去，无的话去首页,
          console.log(this.$route.query.path, 'this.$route.query.path')
          this.$router.replace({
            path: this.$route.query.path || '/Layout/Home',
          })
        }
      } catch (err) {
        this.btnStatus = !this.btnStatus
        console.dir(err)
        Notify({ type: 'danger', message: '登录失败', duration: 500 })
      }
    },
  },
  mounted() {},
  components: {},
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #007bff;
}
/deep/ .van-nav-bar__title {
  color: #fff;
}
</style>
